package cn.rongcloud.im.ui.view;

/**
 * Created by Administrator on 2017/3/17.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author lincheng 2017-3-20 自定义自动换行LinearLayout
 * 这个自定义控件有部分的局域性，是针对这个项目写的，不通用
 */
public class AutomNewLinewView extends ViewGroup {


    public AutomNewLinewView(Context context) {
        super(context);
    }

    public AutomNewLinewView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutomNewLinewView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 控制子控件的换行
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int totalWidth = 0;
        int sizeWidth = r- l;
        int x = 0;
        int y = 0;
        int count = getChildCount();
        boolean addOneColumns ;

        for (int j = 0; j < count; j++) {
            final View childView = getChildAt(j);
            // 获取子控件Child的宽高
            LayoutParams params = childView.getLayoutParams();
            int w = params.width;
            int h = params.height;

            totalWidth = totalWidth + w ;
            if(totalWidth > sizeWidth){
                totalWidth = params.width;
                addOneColumns = true;
            }else{
                addOneColumns= false;
            }

            if (addOneColumns) {
                x = 0;
                y += h;
            }

             int left = x;
             int top = y;

            // 布局子控件
            childView.layout(left, top, left + w, top + h);

            x += w;
        }
    }

    /**
     * 计算控件及子控件所占区域
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 获得此ViewGroup上级容器为其推荐的宽和高，以及计算模式
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        /**
         * 记录如果是wrap_content是设置的宽和高
         */
        int width = 0;
        int height = 0;
        int childWidth = 0;
        int childHeight = 0;
        int totalWidth=0;
        int cCount = getChildCount();
        boolean addOneColumns ;

        /**
         * 根据childView计算的出的宽和高，以及设置的margin计算容器的宽和高，主要用于容器是warp_content时
         */
        for (int i = 0; i < cCount; i++)
        {
            View childView = getChildAt(i);
            childWidth = childView.getMeasuredWidth();
            childHeight = childView.getMeasuredHeight();
            totalWidth = totalWidth +childWidth;
            if(i == 0 ){
                height = childHeight+1;
            }
            if(totalWidth >= sizeWidth){
                totalWidth = childWidth;
                addOneColumns = true;
            }else{
                addOneColumns =false;
            }

            if(addOneColumns){
                height = height +childHeight+1;
            }
        }

        width = sizeWidth;

        /**
         * 如果是wrap_content设置为我们计算的值
         * 否则：直接设置为父容器计算的值
         */
        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth
                : width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight
                : height);
    }
}